<?php
namespace app\index\controller;
// header('content-type:application/json');

use think\Controller;
use think\Db;

class Info extends controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function schoolInfo()
    {
        $sql = Db::name("school")->getLastSql();
        $rs = Db::name("school")->select();
        return json_encode($rs);
    }

    /* 商品列表 */

    public function getGoodsList()
    {
        $tid = input('tid');
        if ($tid == 0) {
            $rs = Db::name('goods')->order('gId', 'desc')
            ->select();
            return json_encode($rs);
        } else {
            $rs = Db::name('goods')->where('typeId',$tid)
            ->order('gId','desc')->select();
            return json_encode($rs);
        }
    }

     /* 促销商品列表 */

    public function getTurnList()
    {
        $tid = input('tid');
        if ($tid == 0) {
            $rs = Db::name('turn')->order('nId', 'desc')
            ->select();
            return json_encode($rs);
        } else {
            $rs = Db::name('turn')->where('typeId',$tid)
            ->order('nId','desc')->select();
            return json_encode($rs);
        }
        
        // $tid = input('tid');
        // if ($tid == 0) {
        //     $rs = Db::name('goods')->order('gId', 'desc')
        //     ->where('gstatus',3)
        //     ->select();
        //     return json_encode($rs);
        // } else {
        //     $rs = Db::name('goods')->where('typeId',$tid)
        //     ->where('gstatus',3)
        //     ->order('gId','desc')->select();
        //     return json_encode($rs);
        // }
    }


    /* 帖子列表 */

    public function getPostsList(){
        $fid = input('fid');
        if ($fid == 0) {
            $rs = Db::name('posts')->order('pId', 'desc')
            ->select();
            return json_encode($rs);
        } else {
            $rs = Db::name('posts')->where('ptypeId',$fid)
            ->order('pId','desc')->select();
            return json_encode($rs);
         }
        // $rs=Db::name('posts')->where('pstatus',1)
        // ->field('pId,pTitle,ptime')
        // ->find();
        // return json_encode($rs);
    }

    /* 测评列表 */

    public function getEvaluaList(){
        $fid = input('fid');
        if ($fid == 0) {
            $rs = Db::name('evalua')->order('eId', 'desc')
            ->select();
            return json_encode($rs);
        } else {
            $rs = Db::name('evalua')->where('etypeId',$fid)
            ->order('eId','desc')->select();
            return json_encode($rs);
         }
        // $rs=Db::name('posts')->where('pstatus',1)
        // ->field('pId,pTitle,ptime')
        // ->find();
        // return json_encode($rs);
    }

    /* 帖子 测评分类 */

    public function ftypes() {
        $rs = Db::name('ftypes')->select();
        array_unshift($rs,[
                            'fId'=>0,
                            'fName'=>"综合"]);
        // dump($rs);
        return json_encode($rs);
    }

    /* 商品分类 */

    public function types() {
        // $rs = Db::name('types')->field('tName')->select();
        $rs = Db::name('types')->select();
        array_unshift($rs,[
                            'tId'=>0,
                            'tName'=>"综合"]);
        // dump($rs);
        return json_encode($rs);
    }

    /* 商品搜索 */

    public function search() {
        // TODO: 正则表达式判断key是否合法
        $key = input('key');
        $rs = Db::name('goods')->whereLike("gTitle|tags",'%'.$key.'%')
        ->select();
        return json_encode($rs);
    }

    /* 商品评论 */

    public function getComments() {
        $gid = input('gid');
        $rs = Db::name('comments c')->where('goodId',$gid)
        ->where('cstatus',0)
        ->join('user u','c.uId=u.userId')
        ->field('cId,cContent,goodId,u.name,cstatus')
        ->select();
        return json_encode($rs);
    }

    /* 帖子评论 */

    public function getPostsComments() {
        $pid = input('pid');
        $rs = Db::name('pcomments c')->where('postsId',$pid)
        ->where('pcstatus',0)
        ->join('user u','c.uId=u.userId')
        ->field('pcId,pcContent,postsId,u.name,pcstatus')
        ->select();
        return json_encode($rs);
    }

    /* 商品详情 */

    public function getGoods() {
        $gid = input('gid');
        $rs = Db::name('goods g')->join('user u','u.userId=g.uid')
        ->where('gId',$gid)
        ->where('gstatus',0)
        ->field('gId,gTitle,gPic,gDetail,typeId,tags,price,vDate,reMark,g.uid,name')
        ->find();
        return json_encode($rs);
    }

    /* 帖子详情 */

    public function getPosts() {
        $pid = input('pid');
        $rs = Db::name('posts p')->join('user u','u.userId=p.uid')
        ->where('pId',$pid)
        ->where('pstatus',1)
        ->field('pId,pTitle,pPic,pContent,ptypeId,ptime,p.uid,name')
        ->find();
        return json_encode($rs);
    }

    /* 轮播图促销活动 */

    public function getTurn(){
        $nid=input('nid');
        // $rs=Db::name('turn')->where('nId',$nid)
        // ->find();
        $rs=Db::name('turn')
        ->where('nId',$nid)
        ->where('tstatus',1)
        ->field('nId,tTittle,tPic,price,promotion,tContent')
        ->find();
        return json_encode($rs);
    }

}
